package com.bc004346.travelus.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bc004346.travelus.R;
import com.bc004346.travelus.model.domain.DayTrip;
import com.bc004346.travelus.utility.Utility;

import java.util.ArrayList;

import static com.bc004346.travelus.utility.Utility.log;

/**
 * Created by Renats on 9/5/2014.
 * Generic * Flight adapter
 */
public class TripAdapter extends ArrayAdapter<DayTrip> {

    private Activity mActivity;

    public TripAdapter(Activity activity, ArrayList<DayTrip> dayTrips) {
        super(activity, 0, dayTrips);
        mActivity = activity;
    }

    @Override
    @SuppressLint("InflateParams")
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = mActivity.getLayoutInflater()
                    .inflate(R.layout.list_item_trip, null);

        DayTrip trip = getItem(position);
        Typeface font = Typeface.createFromAsset(mActivity.getAssets(), "arial.ttf");

        TextView heading = (TextView) convertView.findViewById(R.id.list_item_trip_heading);
        final TextView summary = (TextView) convertView.findViewById(R.id.list_item_trip_summary);
        TextView outboundFlightNumber = (TextView) convertView.findViewById(R.id.list_item_trip_outbound_flight_number);
        TextView outboundTime = (TextView) convertView.findViewById(R.id.list_item_trip_outbound_time);
        TextView outboundSA = (TextView) convertView.findViewById(R.id.list_item_trip_outbound_sa);
        TextView bootsOnGround = (TextView) convertView.findViewById(R.id.list_item_trip_duration_boots_on_the_ground);
        TextView totalTripDuration = (TextView) convertView.findViewById(R.id.list_item_trip_total_duration);
        TextView inboundFlightNumber = (TextView) convertView.findViewById(R.id.list_item_trip_inbound_flight_number);
        TextView inboundTime = (TextView) convertView.findViewById(R.id.list_item_trip_inbound_time);
        TextView inboundSA = (TextView) convertView.findViewById(R.id.list_item_trip_inbound_sa);
        final ViewGroup details = (ViewGroup) convertView.findViewById(R.id.list_item_trip_details);
        final ImageView arrow = (ImageView) convertView.findViewById(R.id.list_item_trip_details_arrow);
        ViewGroup root = (ViewGroup) convertView.findViewById(R.id.list_item_trip_root);

        heading.setTypeface(font);
        outboundFlightNumber.setTypeface(font);
        outboundTime.setTypeface(font);
        outboundSA.setTypeface(font);
        bootsOnGround.setTypeface(font);
        totalTripDuration.setTypeface(font);
        inboundFlightNumber.setTypeface(font);
        inboundTime.setTypeface(font);
        inboundSA.setTypeface(font);

        try {
            heading.setText(String.format("Day Trip to %s",
                    trip.getOutboundFlight().getDestination()));
            summary.setText(String.format("%s in %s, %d and %d seats",
                    trip.getBootOnTheGroundDuration(),
                    trip.getOutboundFlight().getDestination(),
                    trip.getOutboundFlight().getCoach().getSeatsAvailable(),
                    trip.getInboundFlight().getCoach().getSeatsAvailable()));
            outboundFlightNumber.setText(String.format("Flight %d", trip.getOutboundFlight().getFlightNumber()));
            outboundTime.setText(String.format("%s > %s",
                    Utility.MILITARY_TIME.format(trip.getOutboundFlight().getDeparture()),
                    Utility.MILITARY_TIME.format(trip.getOutboundFlight().getArrival())));
            outboundSA.setText(String.format("%d seats", trip.getOutboundFlight().getCoach().getSeatsAvailable()));
            bootsOnGround.setText(String.format("Boots on the ground: %s", trip.getBootOnTheGroundDuration()));
            totalTripDuration.setText(String.format("Total Trip duration: %s", trip.getTripDuration()));
            inboundFlightNumber.setText(String.format("Flight %d", trip.getInboundFlight().getFlightNumber()));
            inboundTime.setText(String.format("%s > %s",
                    Utility.MILITARY_TIME.format(trip.getInboundFlight().getDeparture()),
                    Utility.MILITARY_TIME.format(trip.getInboundFlight().getArrival())));
            inboundSA.setText(String.format("%d seats", trip.getInboundFlight().getCoach().getSeatsAvailable()));

            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(details.getVisibility() == View.VISIBLE) {
                        details.setVisibility(View.GONE);
                        summary.setVisibility(View.VISIBLE);
                        arrow.setRotation(90);
                    }
                    else {
                        details.setVisibility(View.VISIBLE);
                        summary.setVisibility(View.GONE);
                        arrow.setRotation(270);
                    }
                }
            });
        } catch (Exception e) {
            log(e);
        }

        return convertView;
    }
}
